package me.flyray.bsin.server.document.loader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.server.document.split.TextSplitter;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class CodeFileLoader implements ResourceLoader {
  private final TextSplitter textSplitter;

  @Override
  public String getContent(InputStream inputStream) {
    StringBuffer stringBuffer = new StringBuffer();
    try (InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(reader)) {
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line).append("\n");
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return stringBuffer.toString();
  }

  @Override
  public List<String> getChunkList(String content) {
    return textSplitter.split(content);
  }
}
